﻿using MxWeiXinPF.API.Payment.Tenpay;
using MxWeiXinPF.Common;
using MxWeiXinPF.Web.UI;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MxWeiXinPF.Web.weixin.jiaoshuifei
{

    public partial class waterdetail : WeiXinPage
    {
        public List<Model.BindRecord> list = null;
        string key = "";
        string wxusername = "";
        string apiurl = "";
        public int wid = 0;
        public string consno  ="";
        public string consname = "";
        protected void Page_Load(object sender, EventArgs e)
        {
             consno = MyCommFun.QueryString("consno");
             consname = MyCommFun.QueryString("consname");

            wid = MyCommFun.RequestWid();
            string thisUrl = MyCommFun.getWebSite() + "/weixin/jiaoshuifei/waterdetail.aspx?wid=" + wid + "&consname=" + consname + "&consno=" + consno;
            OAuth2BaseProc(wid, thisUrl);


            if (wid == 0 || openid == "")
            {
                Response.Redirect("/weixin/jiaoshuifei/index.aspx?wid=" + wid);
            }

            
            MxWeiXinPF.BLL.shuifei_baseinfo _baseInfoBLL = new BLL.shuifei_baseinfo();
            Model.shuifei_baseinfo baseinfoModel = _baseInfoBLL.GetModelByWid(wid);
            if (baseinfoModel != null)
            {
                wxusername = baseinfoModel.api_UserName;
                key = baseinfoModel.api_Key;
                apiurl = baseinfoModel.api_url;
            }
            string msg = string.Empty;
            list = Water(wxusername, key, consno, "", out msg);
        }
        public List<Model.BindRecord> Water(string wxusername, string key,string consno, string extend, out string msg)
        {
            msg = string.Empty;
            string tradeNo = "1007";
            string datetimeStr = DateTime.Now.ToString("yyyyMMddhhmmssfff");
            StringBuilder strBuf = new StringBuilder();
            strBuf.Append(wxusername);
            strBuf.Append(tradeNo);

            strBuf.Append(consno);
            strBuf.Append(extend);
            strBuf.Append(datetimeStr);

            strBuf.Append(key);
            string sign = MD5Util.GetMD5(strBuf.ToString(), Encoding.UTF8.ToString()).ToUpper();
            SortedDictionary<string, string> sParaTemp = new SortedDictionary<string, string>();
            sParaTemp.Add("consno", consno);
            sParaTemp.Add("extend", extend);
            sParaTemp.Add("randomstr", datetimeStr);
            sParaTemp.Add("tradecode", tradeNo);
            sParaTemp.Add("username", wxusername);
            sParaTemp.Add("sign", sign);

            string para = JsonConvert.SerializeObject(sParaTemp);
            jsfservice.WXWaterServiceSoapClient jsfservice = new jsfservice.WXWaterServiceSoapClient("WXWaterServiceSoap12", apiurl);

            string returnMsg = jsfservice.wxProgress(para);
            if (string.IsNullOrEmpty(returnMsg))
            {
                msg = "returnMsg=null";
                return null;
            }
            Model.JSFResultModel resultModel = JsonConvert.DeserializeObject<Model.JSFResultModel>(returnMsg);
            if (resultModel == null)
            {
                msg = "resultModel=null";
                return null;
            }
            if (resultModel.rtncode != "OK")
            {
                msg = resultModel.rtnmsg;
                return null;
            }

            return resultModel.recordList;
        }
    }
}